<?php
/**更新用户抖音uid
 */

try{
    set_time_limit(0);
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Shanghai');
    require_once "../../db/video_db.php";

    $postData = file_get_contents('php://input');
    $body = json_decode($postData, true);

    $uid  = $body["u"]["uid"];

    $d_uid   = isset($body["s"]["d_uid"])     ? $body["s"]["d_uid"] : 0;//抖音uid

    if(empty($uid)){
        $res = array("code"=>-1, "result"=>null, "msg"=>'用户uid不能为空');
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = "select * from t_user where uid=".$uid;
    $userInfo = video_db::fetch($sql);
    if(empty($userInfo)){
        $res = array("code"=>-1, "result"=>null, "msg"=>'用户不存在');
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    if(empty($d_uid)){
        $res = array("code"=>0, "result"=>array("uid" => $uid), "msg"=>"");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = "update t_user_import set ";
    $set = "";
    if($d_uid){
        $set.= "d_uid=".$d_uid;
    }

//    $set = substr($set, 1);
    $sql.= $set;
    $sql.= " where uid=".$uid;
    video_db::execute($sql);

    video_db::close();
    $res = array("code"=>0, "result"=>array("uid" => $uid), "msg"=>"");
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;

}catch(PDOException $pe){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$pe->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$pe->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}catch (Exception $e){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$e->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$e->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}
